Skip to content

Add EmptyArray and IsEmptyArray types#1355

Open
derodero24 wants to merge 1 commit into
sindresorhus:mainfrom
derodero24:add/empty-array
Open

Add EmptyArray and IsEmptyArray types#1355
derodero24 wants to merge 1 commit into
sindresorhus:mainfrom
derodero24:add/empty-array

Conversation

@derodero24

Copy link
Copy Markdown
Contributor

Closes #929

Adds EmptyArray (never[]) for representing a strictly empty array, analogous to EmptyObject. Also adds IsEmptyArray<T> as a type guard.

IsEmptyArray handles:

  • [], never[], readonly [], readonly never[]true
  • Non-empty tuples, string[], any, neverfalse

This picks up where #931 left off, with the readonly array tests that were requested in review.

Comment thread test-d/empty-array.ts

// IsEmptyArray — Edge cases
expectType<IsEmptyArray<any>>(false);
expectType<IsEmptyArray<never>>(false);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some more tests:

Suggested change
expectType<IsEmptyArray<never>>(false);
expectType<IsEmptyArray<never>>(false);
// IsEmptyArray — Union behavior
expectType<IsEmptyArray<[] | [1]>>({} as boolean);
expectType<IsEmptyArray<readonly [] | readonly [1]>>({} as boolean);
// IsEmptyArray — More non-empty/array boundaries
expectType<IsEmptyArray<[never]>>(false);
expectType<IsEmptyArray<unknown[]>>(false);
expectType<IsEmptyArray<any[]>>(false);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also:

// @ts-expect-error
emptyArray.length = 1;

Comment thread source/empty-array.d.ts Outdated

@category Array
*/
export type EmptyArray = never[];

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not

Suggested change
export type EmptyArray = never[];
export type EmptyArray = [];

?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All addressed — simplified to [], added union/boundary tests, and the length immutability test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

EmptyArray type

2 participants